package com.xq.springboot.controller;

import com.xq.springboot.common.ActiverUser;
import com.xq.springboot.pojo.User;
import com.xq.springboot.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xq
 * @version 1.0
 * @date 2021/4/19 8:48
 * @description 用户控制器
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param user
     * @return
     */
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public Map<String, Object> login(@RequestBody User user) {
        // 创建消息Map容器
        Map<String, Object> map = new HashMap<>(16);
        // 创建token
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(), user.getUserPassword());
        // 创建主体
        Subject subject = SecurityUtils.getSubject();
        try {
            // 登录认证
            subject.login(token);
            // 登录成功，获取subject中ActiverUser对象
            ActiverUser activerUser = (ActiverUser)subject.getPrincipal();
            // 获取token
            Serializable tokenId = subject.getSession().getId();
            // 添加登录成功信息
            map.put("code",200);
            map.put("msg","登录成功");
            map.put("token", tokenId);
            map.put("user",user);
            return map;
        } catch (AuthenticationException e) {
            e.printStackTrace();
            // 添加登录失败信息
            map.put("code",-1);
            map.put("msg","登录失败，用户名或密码不正确");
            return map;
        }
    }

    /**
     * 新增数据
     * @return
     */
    @RequiresPermissions(value = {"user:add"})
    @RequestMapping("add")
    public Map<String,Object> add() {
        Map<String, Object> map = new HashMap<>(16);
        map.put("msg","add");
        return map;
    }
    /**
     * 修改数据
     * @return
     */
    @RequiresPermissions(value = {"user:update"})
    @RequestMapping("update")
    public Map<String,Object> update() {
        Map<String, Object> map = new HashMap<>(16);
        map.put("msg","update");
        return map;
    }


    /**
     * 删除数据
     * @return
     */
    @RequiresPermissions(value = {"user:delete"})
    @RequestMapping("delete")
    public Map<String,Object> delete() {
        Map<String, Object> map = new HashMap<>(16);
        map.put("msg","delete");
        return map;
    }

    /**
     * 查询数据
     * @return
     */
    @RequiresPermissions(value = {"user:query"})
    @RequestMapping("query")
    public Map<String,Object> query() {
        Map<String, Object> map = new HashMap<>(16);
        map.put("msg","query");
        return map;
    }

    /**
     * 导出数据
     * @return
     */
    @RequiresPermissions(value = {"user:export"})
    @RequestMapping("export")
    public Map<String,Object> export() {
        Map<String, Object> map = new HashMap<>(16);
        map.put("msg","export");
        return map;
    }
}
